/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import tw.com.hitevision.whiteboard.android.R;

public class WhiteboardDialog extends FrameLayout {
    private static final String TAG = WhiteboardDialog.class.getSimpleName();

    public static final int BUTTON_POSITIVE = 1;
    public static final int BUTTON_NEGATIVE = 2;

    private TextView title;
    private View titleDivider;
    private TextView content;
    private TextView positive;
    private TextView negative;

    private OnDialogClickListener positiveListener;
    private OnDialogClickListener negativeListener;

    public WhiteboardDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public WhiteboardDialog(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public WhiteboardDialog(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.dialog, this);

        title = findViewById(R.id.title);
        titleDivider = findViewById(R.id.title_divider);
        content = findViewById(R.id.content);
        positive = findViewById(R.id.positive);
        negative = findViewById(R.id.negative);

        positive.setOnClickListener(v -> {
            if (positiveListener != null) {
                positiveListener.onDialogButtonClick(this, BUTTON_POSITIVE);
            }
            setVisibility(View.GONE);
        });

        negative.setOnClickListener(v -> {
            if (negativeListener != null) {
                negativeListener.onDialogButtonClick(this, BUTTON_NEGATIVE);
            }
            setVisibility(View.GONE);
        });

        setOnTouchListener(rootOnTouchListener);
    }

    @SuppressLint("ClickableViewAccessibility")
    private final OnTouchListener rootOnTouchListener = (v, event) -> {
        setVisibility(View.GONE);
        return true;
    };

    public void setTitle(String text) {
        title.setText(text);

        int visibility = text == null || text.isEmpty() ? View.GONE : View.VISIBLE;
        title.setVisibility(visibility);
        titleDivider.setVisibility(visibility);
    }

    public void setContent(String text) {
        content.setText(text);
        this.invalidate();
    }

    public void setPositiveButton(String text, @Nullable OnDialogClickListener listener) {
        positive.setText(text);
        positiveListener = listener;
    }

    public void setNegativeButton(String text, @Nullable OnDialogClickListener listener) {
        negative.setText(text);
        negativeListener = listener;
    }

    public void show() {
        setVisibility(View.VISIBLE);
    }

    public void cancel() {
        setVisibility(View.GONE);
    }

    public interface OnDialogClickListener {
        void onDialogButtonClick(WhiteboardDialog dialog, int button);
    }

    private static void log(String msg) {
        Log.d(TAG, msg);
    }
}
